package com.du.cloud.service.admin.controller;

import com.du.cloud.common.base.utils.Result;
import com.du.cloud.security.annotation.inner.Inner;
import com.du.cloud.security.util.SecurityUtils;
import com.du.cloud.service.admin.api.model.dto.SysUserDTO;
import com.du.cloud.service.admin.service.SysRoleService;
import com.du.cloud.service.admin.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Desc:
 * Author: dushuang
 * Date: 2020/5/25
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserController {

    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysUserService sysUserService;

    @GetMapping(value = "/role/{userId}")
    public Result role(@PathVariable Integer userId) {
        return Result.ok(sysRoleService.findUserRole(userId));
    }

    @GetMapping(value = "/info/{username}")
    @Inner
    public Result info(@PathVariable String username) {
        SysUserDTO user = sysUserService.findByUsername(username);
        if (user == null) {
            return Result.fail(String.format("用户信息为空 %s", username));
        }
        return Result.ok(sysUserService.info(user));
    }

    @GetMapping(value = "current")
    @PreAuthorize("hasAuthority('sys_user')")
    public Result currentUser() {
        return Result.ok(SecurityUtils.getUser());
    }
}
